/**************************************************************************
	Replication do-file: "Corruption in Customs"
	Cyril Chalendard, Ana Fernandes, Gael Raballand and Bob Rijkers
	
	Created on: 01/07/2022
**************************************************************************/

clear all
set more off, perm
cap log close
cls

* ----------------------- DIRECTORIES AND FOLDERS ----------------------- *

* Directories in which data are stored
global main "PUT YOUR DIRECTORY PATH HERE"
cd "$folder"

* Folders
global outputdata = "$main\Output Data"
cap mkdir "$main\Tables"
global tables = "$main\Tables"

* ----------------------------- BEGINS HERE ----------------------------- *

* -------
* Dataset
* -------
use "$outputdata\CFRR.dta", clear // open dataset


* --------
* Table A8
* --------

* Globals
global dep "epsilon sigma relationship_stickiness"

* Matrix
mat tableA8 = J(4,3,.)
mat tableA8_T = J(1,3,.)

* Loop for estimations
local i = 1
local j = 1
foreach x in $dep {
	
	qui: reghdfe `x' eis_f if sample_reg==1 , noab vce(cluster insp_f key_dec)
	
	mat tableA8[`i',`j'] = _b[eis_f]
	local i = `i' + 1
	mat tableA8[`i',`j'] = _se[eis_f]
	mat tableA8_T[1,`j'] = e(N)
	local i = `i' + 2
	mat tableA8[`i',`j'] = e(r2)
	local i = `i' -3
	local j = `j' +1
}


* Export
* ------
putexcel set "$tables\Table A8.xlsx", replace sheet(TableA8) // create a new excel spreadsheet

* Coefficients
putexcel B4 = matrix(tableA8), nformat(0.000) hcenter vcenter
putexcel B6 = matrix(tableA8_T), nformat(number_sep) hcenter vcenter

* Title
putexcel (A1:D1), merge hcenter vcenter
putexcel A1 = "Before delegated randomization of inspector assignment"

* Column names
putexcel A2 = "Dependent variables", left vcenter bold
local cells `" "B" "C" "D" "'
local labels `" "Trade elasticity (Fontagne et al. 2019)" "Trade elasticity (Broda and Weinstein 2006)" "Relationship stickiness (Martin et al. 2020)" "'
forvalues i = 1(1)3 {
	
	local x: word `i' of `labels'
	local y: word `i' of `cells'
	
	putexcel `y'2 = "`x'", hcenter vcenter bold	
	putexcel `y'3 = "(`i')", hcenter vcenter
}

* Row names
putexcel A4 = "Excess interaction share", left vcenter
putexcel A6 = "Observations", left vcenter
putexcel A7 = "R-squared", left vcenter


* -------------------------------- ENDS HERE -------------------------------- *